/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved.
 */
package com.kaaction.modules.operate.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.kaaction.common.config.Global;
import com.kaaction.common.web.BaseController;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.modules.operate.entity.Material;
import com.kaaction.modules.operate.service.MaterialService;

/**
 * 类目Controller
 * @author zengbiao
 * @version 2017-04-13
 */
@Controller
@RequestMapping(value = "${adminPath}/operate/material")
public class MaterialController extends BaseController {

	@Autowired
	private MaterialService materialService;
	
	@ModelAttribute
	public Material get(@RequestParam(required=false) String id) {
		Material entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = materialService.get(id);
		}
		if (entity == null){
			entity = new Material();
		}
		return entity;
	}
	
	@RequiresPermissions("operate:material:view")
	@RequestMapping(value = {"list", ""})
	public String list(Material material, HttpServletRequest request, HttpServletResponse response, Model model) {
		List<Material> list = materialService.findList(material); 
		model.addAttribute("list", list);
		return "modules/operate/materialList";
	}

	@RequiresPermissions("operate:material:view")
	@RequestMapping(value = "form")
	public String form(Material material, Model model) {
		if (material.getParent()!=null && StringUtils.isNotBlank(material.getParent().getId())){
			material.setParent(materialService.get(material.getParent().getId()));
			// 获取排序号，最末节点排序号+30
			if (StringUtils.isBlank(material.getId())){
				Material materialChild = new Material();
				materialChild.setParent(new Material(material.getParent().getId()));
				List<Material> list = materialService.findList(material); 
				if (list.size() > 0){
					material.setSort(list.get(list.size()-1).getSort());
					if (material.getSort() != null){
						material.setSort(material.getSort() + 30);
					}
				}
			}
		}
		if (material.getSort() == null){
			material.setSort(30);
		}
		model.addAttribute("material", material);
		return "modules/operate/materialForm";
	}

	@RequiresPermissions("operate:material:edit")
	@RequestMapping(value = "save")
	public String save(Material material, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, material)){
			return form(material, model);
		}
		materialService.save(material);
		addMessage(redirectAttributes, "保存类目成功");
		return "redirect:"+Global.getAdminPath()+"/operate/material/?repage";
	}
	
	@RequiresPermissions("operate:material:edit")
	@RequestMapping(value = "delete")
	public String delete(Material material, RedirectAttributes redirectAttributes) {
		materialService.delete(material);
		addMessage(redirectAttributes, "删除类目成功");
		return "redirect:"+Global.getAdminPath()+"/operate/material/?repage";
	}

	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(@RequestParam(required=false) String extId, HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<Material> list = materialService.findList(new Material());
		for (int i=0; i<list.size(); i++){
			Material e = list.get(i);
			if (StringUtils.isBlank(extId) || (extId!=null && !extId.equals(e.getId()) && e.getParentIds().indexOf(","+extId+",")==-1)){
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", e.getId());
				map.put("pId", e.getParentId());
				map.put("name", e.getName());
				mapList.add(map);
			}
		}
		return mapList;
	}
	
}